#pragma once
// #include "Calculator.hpp"
#include <iostream>
#include <string>
#include <jsoncpp/json/json.h>
#include <memory>
#include <sstream>
class Request
{
public:
    Request(int x, int y, const char op)
        : _x(x), _y(y), _op(op) {};
    Request() {}
    ~Request() {}
    bool Serialize(std::string &out)
    {
        Json::Value root;
        root["_x"] = _x;
        root["_y"] = _y;
        root["_op"] = _op;

        Json::StreamWriterBuilder builder;
        std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
        std::stringstream ss;
        writer->write(root, &ss);
        out = ss.str();
        return true;
    }
    bool Deserialize(std::string &in)
    {
       
        Json::Value root;
        Json::Reader reader;
        bool parese_success = reader.parse(in, root);
        if (!parese_success)
        {
            std::cout << "Json parse error" << reader.getFormattedErrorMessages() << std::endl;
            return false;
        }
        _x = root["_x"].asInt();
        _y = root["_y"].asInt();
        _op = root["_op"].asInt();
        std::cout<<"aaabbbbbbbbbbbbbbbbbbbbbb"<<"\n";
        return true;
    }
    void Print()
    {
        std::cout << "Request: x=" << _x << " y=" << _y << " op=" << _op << std::endl;
    }
    int X()
    {
        return _x;
    }
    int Y()
    {
        return _y;
    }
    char Op()
    {
        return _op;
    }

private:
    int _x, _y;
    char _op;
};

class Response
{
public:
    Response() {}
    Response(double result, int error_code)
        : _result(result), _error_code(error_code) {};
    bool Serialize(std::string &out)
    {
        Json::Value root;
        root["_result"] = _result;
        root["_error_code"] = _error_code;
        Json::StreamWriterBuilder builder;
        std::unique_ptr<Json::StreamWriter> Writer(builder.newStreamWriter());
        std::stringstream ss;
        Writer->write(root, &ss);
        out = ss.str();
        return true;
    }
    bool Deserialize(std::string &in)
    {
        Json::Value root;
        Json::Reader reader;
        bool parse_success = reader.parse(in, root);
        _result = root["_result"].asDouble();
        _error_code = root["_error_code"].asInt();
        return true;
    }
    double Result()
    {
        return _result;
    }
    int ErrorCode()
    {
        return _error_code;
    }
    ~Response() {};
    void SetResult(double result)
    {
        _result = result;
    }
    void SetErrorCode(int error_code)
    {
        _error_code = error_code;
        }
private:
    double _result;
    int _error_code;
};